/**
 * Created by sachuangwei on 2017-07-20.
 */
var httpService = require('../../service/httpService')
var Mongodb = require('../../database/mongodb');
let http = httpService.getHttpInstance()
/**
 * 统一的登录方法
 * @param { String } loginName
 * @param { String } password
 */
function login(params) {
  return loginPromise(encodeURIComponent(params.loginName), encodeURIComponent(params.password));
}

/**
 * 原来的登录接口
 * @param {*} userName
 * @param {*} password
 * @returns
 */
function loginPromise(userName, password) {
  const params = {
    userName,
    password,
    loginType: 1,
    deviceId: '00%3A15%3A5d%3A04%3A0f%3A00'
  };
  const url = '/login';
  return http.postFormData(url, params, {
    accessToken: '',
    headers: {
      'FORGED-USER-AGENT': 'pcclient'
    }
  });
}

/**
 * 获取推送CC消息Token
 */
function getCCToken() {
  let condition = {
    email: 'sachuangwei@ygsoft.com'
  }
  return new Promise((resolve, reject) => {
    Mongodb.findOne('users', condition, function (err, docs) {
      if (err) {
        reject(err)
      } else {
        loginPromise(docs.email, docs.password).then(res => {
          if (res.data.code === 0) {
            resolve(res.data.body.ecpToken);
          } else {
            reject(new Error(res.data.message))
          }
        })
      }
    })
  })
}

/**
 * 原来的登录接口
 * @param {*} userName
 * @param {*} password
 * @returns
 */
function getAppId() {
  let condition = {
    "csName": "CC_APPID"
  }
  return new Promise((resolve, reject) => {
    Mongodb.findOne('xtcs', condition, function (err, docs) {
      if (err) {
        reject(err)
      } else {
        resolve(docs.csValue)
      }
    })
  })
}

/**
 * 发送cici机器人消息.
 * @param {*} toUserEmail
 * @param {*} content
 * @returns
 */
async function sendMessage(toUserEmail, content, linkUrl = "https://test/works/#/dashboard/board") {
  let token = await getCCToken();
  let appid = await getAppId()
  let post_data = {
    "robotKey": appid,
    "emails": [toUserEmail],
    "content": {
      "cardTitleVo": {
        "text": "EasyWork消息提醒",
        "icon": "2942c978613d49e3934ef8820e94afaa"
      },
      "cardLinkVo": {
        "pc": {
          "url": linkUrl,
          "type": 'remote'
        },
        "app": {
          "url": linkUrl,
          "type": 'remote'
        }
      },
      "isLink": true,
      "messageVos": [{
        type: 'text',
        content: [content]
      }]
    }
  }
  return http.post('/open/chatItem/sendCard', post_data, {
    headers: {
      'Cookie': 'ecpDataContext.tokenId=' + token
    }
  })
}

/**
 * 发送cici机器人消息.
 * @param {*} toUserEmail
 * @param {*} content
 * @returns
 */
function getContextByToken(token) {
  let params = {
    "userToken": token
  }
  const url = '/auth/loginByToken';
  return new Promise((resolve, reject) => {
    http.post(url, params).then(res => {
      resolve(res.data.body.userName)
    });
  })
}

module.exports = {
  login: login,
  sendMessage: sendMessage,
  getContextByToken: getContextByToken
}